package com.wit.witmg.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.wit.witmg.entity.TCom;
import com.wit.witmg.mapper.TComMapper;
import com.wit.witmg.service.ITComService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wit.witmg.util.ErrorCode;
import com.wit.witmg.util.JsonResult;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.TimeZone;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author bob
 * @since 2025-09-19
 */
@Service
public class TComServiceImpl extends ServiceImpl<TComMapper, TCom> implements ITComService {

    @Override
    public Object add(HttpServletRequest request, String command) {
        TCom t=new TCom();
        t.setCommand(command);
        t.setDate(new Date());
        if(save(t)){
            return JsonResult.success();
        }
        return JsonResult.error(ErrorCode.ERR_504);
    }

    // hi、lo 各是 16 位寄存器值；设备字节序为 B A D C（从 MSB 到 LSB）
    private float floatFromBADC(int hi, int lo) {
        int A = (hi >>> 8) & 0xFF;
        int B =  hi        & 0xFF;
        int C = (lo >>> 8) & 0xFF;
        int D =  lo        & 0xFF;

        // 直接拼出 IEEE-754 的 32 位比特（按大端：B A D C）
        int bits = (B << 24) | (A << 16) | (D << 8) | C;
        return Float.intBitsToFloat(bits);
    }


    @Override
    public Object test(HttpServletRequest request, String command) {
        String json="{\"date\":\"2025-09-23 01:14:15\"}";
        TCom t1= JSONObject.parseObject(json,TCom.class);
        t1.setCommand("111");
        save(t1);
        TCom t=new TCom();
        t.setCommand(command);
        t.setDate(new Date());
        save(t);
        return "当前时区: " + TimeZone.getDefault().getID() +
                ", 显示名称: " + TimeZone.getDefault().getDisplayName() +
                ", 当前时间: " + new Date();
    }
}
